/*
 * @Author: pengyong
 * @Date: 2021-03-12 22:25:08
 * @LastEditors: pengyong
 * @LastEditTime: 2021-03-15 21:03:24
 * @Description: 修改密码
 * @FilePath: /supplier_app/screens/ChangePasswordScreen.js
 */
import React, {Component} from 'react';
import {
  SafeAreaView,
  ImageBackground,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  TextInput,
} from 'react-native';

import {Input, Button} from 'react-native-elements';
import {Actions} from 'react-native-router-flux';
import AsyncStorage from '@react-native-community/async-storage';

import Api from '../Api';
import AvatarIcon from '../assets/avatar.png';
import {showToast} from '../utils';

export default class ChangePassword extends Component {
  constructor(props) {
    super(props);
    this.state = {
      oldPassword: '',
      newPassword: '',
      newPasswordAgain: '',
    };
  }
  componentDidMount() {}
  handleSubmit = () => {
    const {oldPassword, newPassword, newPasswordAgain} = this.state;
    if (!oldPassword) {
      showToast('请输入旧密码');
      return;
    }
    if (!newPassword) {
      showToast('请输入新密码');
      return;
    }
    if (!newPasswordAgain) {
      showToast('请再次输入新密码');
      return;
    }
    if (newPassword !== newPasswordAgain) {
      showToast('两次新密码输入不一致');
      return;
    }
    Api.changePassword({
      newPass: newPassword,
      password: oldPassword,
    }).then(res => {
      if (res.status === 200) {
        showToast('修改成功，请重新登录');
        setTimeout(() => {
          Api.logout();
        }, 500);
      }
    });
  };
  render() {
    return (
      <SafeAreaView style={{flex: 1}}>
        <View style={styles.formStyle}>
          <Input
            label="旧密码"
            placeholder="请输入旧密码"
            labelStyle={styles.labelStyle}
            onChangeText={value => this.setState({oldPassword: value})}
            secureTextEntry
          />
          <Input
            label="新密码"
            placeholder="请输入新密码"
            labelStyle={styles.labelStyle}
            onChangeText={value => this.setState({newPassword: value})}
            secureTextEntry
          />
          <Input
            label="新密码"
            placeholder="请再次输入新密码"
            labelStyle={styles.labelStyle}
            onChangeText={value => this.setState({newPasswordAgain: value})}
            secureTextEntry
          />
        </View>
        <View style={styles.submitBot}>
          <Button
            title="确定"
            buttonStyle={styles.submitBtn}
            onPress={this.handleSubmit}
          />
        </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  formStyle: {
    marginTop: 30,
  },
  labelStyle: {
    color: '#333',
  },
  submitBot: {
    paddingHorizontal: 10,
    paddingVertical: 8,
  },
  submitBtn: {
    height: 50,
    backgroundColor: '#5096fc',
    marginTop: 30,
  },
});
